package com.mytinyRPC.framework.loadBalance.Impl;


import com.mytinyRPC.Factory.SingletonFactory;
import com.mytinyRPC.framework.URL;
import com.mytinyRPC.framework.loadBalance.LoadBalance;
import com.mytinyRPC.register.ServiceDiscovery;
import com.mytinyRPC.register.zookeeperRegister.ZkServiceDiscovery;

import java.util.List;

/***
 * @Author HP
 * @Description //TODO 轮询负载均衡
 **/
public class RoundLoadBalance implements LoadBalance {

    //初始游标
    private int choose = -1;
    private static final ServiceDiscovery serviceDiscovery;
    static {
        serviceDiscovery = SingletonFactory.getInstance(ZkServiceDiscovery.class);
    }

    @Override
    public URL loadBalance(String interfaceName) {
        List<URL> urlList= serviceDiscovery.getURL(interfaceName);
        choose++;
        choose = choose%urlList.size();
        System.out.println("选择负载服务器:"+ urlList.get(choose).toString());
        return urlList.get(choose);
    }
}
